package gw.com.android.ui.chart.manager;

import android.content.Context;
import android.content.res.Configuration;
import android.view.View;

import com.gwtsz.chart.output.bean.DataParse;
import com.gwtsz.chart.output.bean.KLineBean;
import com.gwtsz.chart.output.customer.MyCombinedChart;
import com.gwtsz.chart.output.utils.GTTIndicatorsModel;
import com.gwtsz.chart.output.utils.GTTKDataModel;
import com.gwtsz.chart.output.utils.Periodicity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import www.com.library.app.Logger;
import www.com.library.util.ProductPrecision;

/**
 * Created by spence on 17-3-16.
 */

public class ChartKViewImp implements ChartKViewInterface {

  private static String TAG=ChartKViewImp.class.getSimpleName();

  private static String METHOD="class="+TAG+" method=";

  private ArrayList<GTTKDataModel> kDataList;

  private ProductPrecision mPrecision;

  private MyCombinedChart mMyCombinedchart;

  private MyCombinedChart mViceChartchart;

  private Context mContext;
  /** 主图控制 **/
  private ChartInterface chartViewControl;
  /** 主图指标 **/
  private GTTIndicatorsModel mIndicatorsModel;
  /** 副图指标 **/
  private GTTIndicatorsModel mViceIndicatorsModel;
  /** 产品code */
  public int uiCode = 0;
  /** k线类型 */
  public int nativeKLineType;
  /**交易所 */
  public int zoneType;


  public ChartKViewImp(MyCombinedChart myCombinedchart, MyCombinedChart ViceCombinedchart,Context context){
    mMyCombinedchart=myCombinedchart;
    mViceChartchart=ViceCombinedchart;
    mContext=context;
    chartViewControl=CandleControl.Build(context,myCombinedchart,ViceCombinedchart);
    init();
  }

  private void init(){
    /*mMyCombinedchart.setListener(new com.gwtsz.chart.listener.OnChartDataListenner() {
      @Override
      public void updata(int fristTime) {
        if (!ReqDataUtils.instance().isReqStatus){
          GTTKDataModel model = GTTKDataManager.instance().getKLineModel(0);
          if(null != model) {
            ReqDataUtils.instance().reqKLineData(uiCode, zoneType, nativeKLineType, model.priceTime);
          }
        }
      }
    });*/
  }

  @Override
  public void setProductPrecision(ProductPrecision precision) {
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName()+",precision="+precision);
    mPrecision=precision;
  }

  @Override
  public void refreshData(int uiCode, int zoneType, int nativeKLineType) {
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName()+",uiCode="+uiCode+",zoneType="+zoneType+",nativeKLineType="+nativeKLineType);
    this.uiCode = uiCode;
    this.zoneType = zoneType;
    this.nativeKLineType = nativeKLineType;
    invalidate();
  }

  @Override
  public void invalidate() {
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName());
    refreshDataToViewControl();
    /*mMyCombinedchart.notifyDataSetChanged();
    mViceChartchart.notifyDataSetChanged();
    mMyCombinedchart.invalidate();
    mViceChartchart.invalidate();*/
  }

  /**
   * 刷新数据
   */
  private void refreshDataToViewControl(){
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName());
    try {
      DataParse dataParse=new DataParse();
      dataParse.parseKLine(changeData());
      dataParse.setmSuperIndicators(mIndicatorsModel);
      dataParse.setmViceIndicators(mViceIndicatorsModel);
      dataParse.setkModel(kDataList);
      dataParse.setNativeKLineType(nativeKLineType);
      chartViewControl.setProductDecimalNum(mPrecision.mNumOfDecimalsPoint);
      chartViewControl.refreshData(dataParse);

    }catch (JSONException e){

    }
  }

  @Override
  public void addNewData(KLineBean kLineBean) {
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName()+",kLineBean="+kLineBean);
    chartViewControl.addDataToFoot(kLineBean,mIndicatorsModel,mViceIndicatorsModel);
  }

  @Override
  public void refreshNewData(KLineBean kLineBean) {
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName()+",kLineBean="+kLineBean);
    chartViewControl.refreshFootData(kLineBean, mIndicatorsModel,mViceIndicatorsModel);
  }

  @Override
  public boolean getNowPriceState() {
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName());
    return  mMyCombinedchart.getNowPrice();
  }

  @Override
  public void setDataList(ArrayList<GTTKDataModel> list) {
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName());
    kDataList=(ArrayList<GTTKDataModel>) list.clone();
  }

  /**
   * 将数据转换为图表数据
   */
  public JSONObject changeData() throws JSONException {
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName());
    JSONArray jsonArray1=new JSONArray();
    int i=0;
    for (GTTKDataModel gttkDataModel:kDataList){
      JSONArray jsonArray=new JSONArray();
      jsonArray.put(changeType(gttkDataModel.priceTime));
      jsonArray.put(Float.parseFloat(gttkDataModel.openPrice+""));
      jsonArray.put(Float.parseFloat(gttkDataModel.lastPrice+""));
      jsonArray.put(Float.parseFloat(gttkDataModel.highPrice+""));
      jsonArray.put(Float.parseFloat(gttkDataModel.lowPrice+""));
      jsonArray.put(Float.parseFloat(gttkDataModel.changePrice+""));
      jsonArray1.put(jsonArray);
    }
    JSONObject jsondata=new JSONObject();
    jsondata.put("day",jsonArray1);
    JSONObject jsonsz0022081=new JSONObject();
    jsonsz0022081.put("sz002081",jsondata);
    String jsonCodeString = "{'code':0}";
    JSONObject jsoncode=new JSONObject(jsonCodeString);
    jsoncode.put("data",jsonsz0022081);
    return jsoncode;
  }

  /**
   * 根据类型输出日期格式
   * @param time
   * @return
   */
  private  String changeType(long time){
    if(nativeKLineType== Periodicity.K_LINE_TYPE_MONTH){
      return changeTimeToYYYYMMDD(time);
    }else if(nativeKLineType==Periodicity.K_LINE_TYPE_WEEK){
      return changeTimeToYYYYMMDD(time);
    }else if(nativeKLineType==Periodicity.K_LINE_TYPE_DAY){
      return changeTimeToYYYYMMDD(time);
    }else if(nativeKLineType==Periodicity.K_LINE_TYPE_4HOUR){
      return changeTimeToMMDDhhmm(time);
    }else if(nativeKLineType==Periodicity.K_LINE_TYPE_2HOUR){
      return changeTimeToMMDDhhmm(time);
    }else if(nativeKLineType==Periodicity.K_LINE_TYPE_60MIN){
      return changeTimeToMMDDhhmm(time);
    }else if(nativeKLineType==Periodicity.K_LINE_TYPE_30MIN){
      return changeTimeToMMDDhhmm(time);
    }else if(nativeKLineType== Periodicity.K_LINE_TYPE_15MIN){
      return changeTimeToMMDDhhmm(time);
    }else if(nativeKLineType== Periodicity.K_LINE_TYPE_5MIN){
      return changeTimeToMMDDhhmm(time);
    }else if(nativeKLineType== Periodicity.K_LINE_TYPE_1MIN){
      return changeTimeToMMDDhhmm(time);
    }else
      return null;
  }

  private String changeTimeToYYYYMMDD(long time){
    Date date=new Date(time*1000);
    return new SimpleDateFormat("yyyy/MM/dd").format(date);
  }

  private String changeTimeToMMDDhhmm(long time){
    Date date=new Date(time*1000);
    return new SimpleDateFormat("yyyy/MM/dd HH:mm").format(date);
  }

  @Override public void setViceIndicator(GTTIndicatorsModel gttIndicatorsModel) {
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName()+",gttIndicatorsModel="+gttIndicatorsModel);
    mViceIndicatorsModel=gttIndicatorsModel;
  }

  /**
   * 横竖屏判断
   * @return
   */
  public boolean isScreenChange() {
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName());
    Configuration mConfiguration = mContext.getResources().getConfiguration(); //获取设置的配置信息
    int ori = mConfiguration.orientation ; //获取屏幕方向

    if(ori == mConfiguration.ORIENTATION_LANDSCAPE){

      //横屏
      return true;
    }else if(ori == mConfiguration.ORIENTATION_PORTRAIT){

      //竖屏
      return false;
    }
    return false;
  }

  @Override public void setVisibility(boolean select) {
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName()+",select="+select);
    if(select) {
      mViceChartchart.setVisibility(View.VISIBLE);
    }else {
      mViceChartchart.setVisibility(View.GONE);
    }
    mMyCombinedchart.showXAxis(!select);
  }

  @Override public void refreshIndicator() {
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName());
    chartViewControl.refreshData(mIndicatorsModel,true);
    chartViewControl.refreshData(mViceIndicatorsModel,false);
  }

  @Override public void setPaintStype() {
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName());
    chartViewControl.refreshCandleData();
  }

  @Override public void setMainIndicator(GTTIndicatorsModel gttIndicatorsModel) {
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName()+",gttIndicatorsModel="+gttIndicatorsModel);
    mIndicatorsModel=gttIndicatorsModel;
  }

  @Override public void refreshDecimalNum(int num) {
    Logger.i("ChartLibraryLogInfo",METHOD+Thread.currentThread().getStackTrace()[2].getMethodName()+",num="+num);
    //chartViewControl.setProductDecimalNum(num);
    chartViewControl.refreshProductDecimalNum(num);
  }

}

